package com.xiaoxu.intranetweb.mybatisplusSzappdb.business.product;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.dao.UserInfoDao;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.entity.*;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.service.ICCompanysService;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.service.ICPdtsAuthService;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.service.ICProductsInfoService;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.service.ITPriceRecordService;
import com.xiaoxu.intranetweb.util.SzResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

//设置预调价的业务类
@Component
public class SpecialOfferBu {
    @Autowired
    private UserInfoDao userInfoDao;
    @Autowired
    private ICPdtsAuthService icPdtsAuthService;
    @Autowired
    private ICProductsInfoService  productsInfoService;
    @Autowired
    private ITPriceRecordService  tPriceRecordService;
    @Autowired
    private ICCompanysService  icCompanysService;
    //    设置特价（直接覆盖原价执行特价）
    public SzResult setPrePrice1(String id, String price, String startTime, String token,String plantId) throws Exception {
        LocalDateTime now = LocalDateTime.now();
        SysUser sysUser = userInfoDao.getUser(StpUtil.getLoginIdByToken(token).toString());
        QueryWrapper<CPdtsAuth> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",id);
        CPdtsAuth cPdtsAuth1 = icPdtsAuthService.getOne(queryWrapper);
        CPdtsAuth cPdtsAuth = new CPdtsAuth();
//        表示是否设置特价销售区间
        cPdtsAuth.setPdtsauthPreCmd(BigDecimal.valueOf(1));
        cPdtsAuth.setPdtsauthPrePrice(BigDecimal.valueOf(Double.parseDouble(price)));
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime parsedTime = LocalDateTime.parse(startTime, formatter);
        cPdtsAuth.setPdtsauthPreStartTime(parsedTime);
//        PDTSAUTH_OPRTR
        cPdtsAuth.setPdtsauthPreOprtr(sysUser.getTrueName());
        cPdtsAuth.setPdtsauthPreTime(LocalDateTime.now());
        icPdtsAuthService.update(cPdtsAuth,queryWrapper);
        saveTPriceRecord1(price,sysUser,cPdtsAuth1,now,parsedTime,1,plantId);
        return SzResult.Success();
    }
    //    设置区间价格（优先级比特价更高）
    public SzResult setPrePrice2(String id,String price,String startTime,String token,String plantId) throws Exception {
        LocalDateTime now = LocalDateTime.now();
        SysUser sysUser = userInfoDao.getUser(StpUtil.getLoginIdByToken(token).toString());
        QueryWrapper<CPdtsAuth> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",id);
        CPdtsAuth cPdtsAuth1 = icPdtsAuthService.getOne(queryWrapper);
        CPdtsAuth cPdtsAuth = new CPdtsAuth();
//        表示是否设置特价销售区间
        cPdtsAuth.setPdtsauthPreCmd2(BigDecimal.valueOf(1));
        cPdtsAuth.setPdtsauthPrePrice2(BigDecimal.valueOf(Double.parseDouble(price)));
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

        LocalDateTime parsedTime = LocalDateTime.parse(startTime, formatter);
        cPdtsAuth.setPdtsauthPreStartTime2(parsedTime);
        //        PDTSAUTH_OPRTR
        cPdtsAuth.setPdtsauthPreOprtr2(sysUser.getTrueName());
        cPdtsAuth.setPdtsauthPreTime2(now);
        icPdtsAuthService.update(cPdtsAuth,queryWrapper);
        saveTPriceRecord1(price,sysUser,cPdtsAuth1,now,parsedTime,2,plantId);
        return SzResult.Success();
    }
    //    setAllPrePrice1产品一键预调价1
    public SzResult setAllPrePrice1( String pdtId,String price, String startTime, String token, String plantId) throws Exception {
        LocalDateTime now = LocalDateTime.now();
        SysUser sysUser = userInfoDao.getUser(StpUtil.getLoginIdByToken(token).toString());
        QueryWrapper<CProductsInfo> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("ID", pdtId);
        queryWrapper1.eq("DATA_FROM", plantId);
        CProductsInfo one = productsInfoService.getOne(queryWrapper1);
        QueryWrapper<CPdtsAuth> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("PDTSAUTH_PDT", one.getPdtinfPdt());
        queryWrapper.eq("DATA_FROM", plantId);
        queryWrapper.eq("PDTSAUTH_DELETED", 0);
//        拿出全部授权这个品种的授权信息
        List<CPdtsAuth> list = icPdtsAuthService.list(queryWrapper);
        CPdtsAuth cPdtsAuth = new CPdtsAuth();
        cPdtsAuth.setPdtsauthPreCmd(BigDecimal.valueOf(1));
        cPdtsAuth.setPdtsauthPrePrice(BigDecimal.valueOf(Double.parseDouble(price)));
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime parsedTime = LocalDateTime.parse(startTime, formatter);
        cPdtsAuth.setPdtsauthPreStartTime(parsedTime);
        cPdtsAuth.setPdtsauthPreTime(now);
        for (CPdtsAuth cPdtsAuth1 : list) {
            cPdtsAuth.setId(cPdtsAuth1.getId());
            cPdtsAuth.setDataFrom(plantId);
            cPdtsAuth.setPdtsauthPreOprtr(sysUser.getTrueName());
            cPdtsAuth.setPdtsauthDeleted(cPdtsAuth1.getPdtsauthDeleted());
            icPdtsAuthService.update(cPdtsAuth, queryWrapper);
            saveTPriceRecord2(price,sysUser,cPdtsAuth1,now,parsedTime,1,plantId);
        }

        return SzResult.Success();
    }

    //    setAllPrePrice1产品一键预调价2
    public SzResult setAllPrePrice2( String pdtId,String price, String startTime,  String token, String plantId) throws Exception {
        LocalDateTime now = LocalDateTime.now();
        SysUser sysUser = userInfoDao.getUser(StpUtil.getLoginIdByToken(token).toString());
        QueryWrapper<CProductsInfo> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("ID", pdtId);
        queryWrapper1.eq("DATA_FROM", plantId);
        CProductsInfo one = productsInfoService.getOne(queryWrapper1);
        QueryWrapper<CPdtsAuth> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("PDTSAUTH_PDT", one.getPdtinfPdt());
        queryWrapper.eq("DATA_FROM", plantId);
        queryWrapper.eq("PDTSAUTH_DELETED", 0);
//        ���出全部��这个品种的��信息
        List<CPdtsAuth> list = icPdtsAuthService.list(queryWrapper);
        CPdtsAuth cPdtsAuth = new CPdtsAuth();
        cPdtsAuth.setPdtsauthPreCmd2(BigDecimal.valueOf(1));
        cPdtsAuth.setPdtsauthPrePrice2(BigDecimal.valueOf(Double.parseDouble(price)));
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime parsedTime = LocalDateTime.parse(startTime, formatter);
        cPdtsAuth.setPdtsauthPreStartTime2(parsedTime);
        cPdtsAuth.setPdtsauthPreTime2(now);
        for (CPdtsAuth cPdtsAuth1 : list) {
            cPdtsAuth.setId(cPdtsAuth1.getId());
            cPdtsAuth.setDataFrom(plantId);
            cPdtsAuth.setPdtsauthPreOprtr2(sysUser.getTrueName());
            cPdtsAuth.setPdtsauthDeleted(cPdtsAuth1.getPdtsauthDeleted());
            icPdtsAuthService.update(cPdtsAuth, queryWrapper);
            saveTPriceRecord2(price,sysUser,cPdtsAuth1,now,parsedTime,2,plantId);
        }

        return SzResult.Success();
    }

    //    产品调价记录
//      QueryWrapper<TPriceRecord> queryWrapper2 = new QueryWrapper<>();
//                queryWrapper2.like("RECORD_NO", recordNo);
//                queryWrapper2.orderByDesc("RECORD_NO");
//                queryWrapper2.last("limit 1");
//                TPriceRecord tPriceRecord1 = itPriceRecordService.getOne(queryWrapper2);
//                TPriceRecord tPriceRecord = new TPriceRecord();
//                tPriceRecord.setPrice(BigDecimal.valueOf(Double.parseDouble(price)));
//                QueryWrapper<CProductsInfo> queryWrapper = new QueryWrapper<>();
//                queryWrapper.eq("PDTINF_PDT", cPdtsAuth.getPdtsauthPdt());
//                queryWrapper.eq("PDTINF_DELETED", 0);
//                CProductsInfo cProductsInfo = icProductsInfoService.getOne(queryWrapper);
//                tPriceRecord.setPdtId(cProductsInfo.getId());
//                tPriceRecord.setCreateTime(LocalDateTime.now());
//                tPriceRecord.setCreatorId(user.getId());
//                QueryWrapper<CCompanys> queryWrapper1 = new QueryWrapper<>();
//                queryWrapper1.eq("CPY_CO", cPdtsAuth.getPdtsauthCo());
//                queryWrapper1.eq("CPY_CHICO", cPdtsAuth.getPdtsauthChico());
//                queryWrapper1.eq("CPY_DELETED", 0);
//                CCompanys cCompanys = icCompanysService.getOne(queryWrapper1);
//                tPriceRecord.setCpyId(cCompanys.getId());
//                tPriceRecord.setCreatorName(user.getTrueName());
//                if (tPriceRecord1 == null) {
//                    tPriceRecord.setRecordNo(BigDecimal.valueOf(Long.parseLong(recordNo + "0001")));
//                } else {
//                    tPriceRecord.setRecordNo(BigDecimal.valueOf(Long.parseLong(tPriceRecord1.getRecordNo().toString()) + 1));
//                }
//                tPriceRecord.setDataFrom(plantId);
//                itPriceRecordService.save(tPriceRecord);
    public void saveTPriceRecord1(String price,SysUser user,CPdtsAuth cPdtsAuth ,
                                  LocalDateTime creatTime,
                                  LocalDateTime executionTime,
                                  Integer order,String plantId){
        String recordNo = LocalDateTime.now().getYear() + String.valueOf(LocalDateTime.now().getMonthValue() < 10 ? "0" + LocalDateTime.now().getMonthValue() : LocalDateTime.now().getMonthValue()) + (LocalDateTime.now().getDayOfMonth() < 10 ? "0" + LocalDateTime.now().getDayOfMonth() : LocalDateTime.now().getDayOfMonth());
        QueryWrapper<TPriceRecord> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.like("RECORD_NO", recordNo);
        queryWrapper2.orderByDesc("RECORD_NO");
        queryWrapper2.last("limit 1");
        TPriceRecord tPriceRecord1 = tPriceRecordService.getOne(queryWrapper2);
        TPriceRecord tPriceRecord = new TPriceRecord();
        tPriceRecord.setPrice(BigDecimal.valueOf(Double.parseDouble(price)));
        QueryWrapper<CProductsInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("PDTINF_PDT", cPdtsAuth.getPdtsauthPdt());
        queryWrapper.eq("PDTINF_DELETED", 0);
        CProductsInfo cProductsInfo = productsInfoService.getOne(queryWrapper);
        tPriceRecord.setPdtId(cProductsInfo.getId());
        tPriceRecord.setCreateTime(creatTime);
        tPriceRecord.setCreatorId(user.getId());
        QueryWrapper<CCompanys> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("CPY_CO", cPdtsAuth.getPdtsauthCo());
        queryWrapper1.eq("CPY_CHICO", cPdtsAuth.getPdtsauthChico());
        queryWrapper1.eq("CPY_DELETED", 0);
        CCompanys cCompanys = icCompanysService.getOne(queryWrapper1);
        tPriceRecord.setCpyId(cCompanys.getId());
        tPriceRecord.setExecutionTime(executionTime);
        tPriceRecord.setCreatorName(user.getTrueName());
        if (tPriceRecord1 == null) {
            tPriceRecord.setRecordNo(BigDecimal.valueOf(Long.parseLong(recordNo + "0001")));
        } else {
            tPriceRecord.setRecordNo(BigDecimal.valueOf(Long.parseLong(tPriceRecord1.getRecordNo().toString()) + 1));
        }
        tPriceRecord.setRemark("产品预调价"+order+"，品种【"+cProductsInfo.getPdtinfPdt()+"】"+"，开始执行时间【"+executionTime+"】"+"，执行价格【"+price+"】");
        tPriceRecord.setDataFrom(plantId);
        tPriceRecordService.save(tPriceRecord);
    }

    public void saveTPriceRecord2(String price,SysUser user,CPdtsAuth cPdtsAuth ,
                                  LocalDateTime creatTime,
                                  LocalDateTime executionTime,
                                  Integer order,String plantId){
        String recordNo = LocalDateTime.now().getYear() + String.valueOf(LocalDateTime.now().getMonthValue() < 10 ? "0" + LocalDateTime.now().getMonthValue() : LocalDateTime.now().getMonthValue()) + (LocalDateTime.now().getDayOfMonth() < 10 ? "0" + LocalDateTime.now().getDayOfMonth() : LocalDateTime.now().getDayOfMonth());
        QueryWrapper<TPriceRecord> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.like("RECORD_NO", recordNo);
        queryWrapper2.orderByDesc("RECORD_NO");
        queryWrapper2.last("limit 1");
        TPriceRecord tPriceRecord1 = tPriceRecordService.getOne(queryWrapper2);
        TPriceRecord tPriceRecord = new TPriceRecord();
        tPriceRecord.setPrice(BigDecimal.valueOf(Double.parseDouble(price)));
        QueryWrapper<CProductsInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("PDTINF_PDT", cPdtsAuth.getPdtsauthPdt());
        queryWrapper.eq("PDTINF_DELETED", 0);
        CProductsInfo cProductsInfo = productsInfoService.getOne(queryWrapper);
        tPriceRecord.setPdtId(cProductsInfo.getId());
        tPriceRecord.setCreateTime(creatTime);
        tPriceRecord.setCreatorId(user.getId());
        QueryWrapper<CCompanys> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("CPY_CO", cPdtsAuth.getPdtsauthCo());
        queryWrapper1.eq("CPY_CHICO", cPdtsAuth.getPdtsauthChico());
        queryWrapper1.eq("CPY_DELETED", 0);
        CCompanys cCompanys = icCompanysService.getOne(queryWrapper1);
        tPriceRecord.setCpyId(cCompanys.getId());
        tPriceRecord.setCreatorName(user.getTrueName());
        if (tPriceRecord1 == null) {
            tPriceRecord.setRecordNo(BigDecimal.valueOf(Long.parseLong(recordNo + "0001")));
        } else {
            tPriceRecord.setRecordNo(BigDecimal.valueOf(Long.parseLong(tPriceRecord1.getRecordNo().toString()) + 1));
        }
        tPriceRecord.setDataFrom(plantId);
        tPriceRecord.setRemark("一键预调价"+order+"，品种【"+cProductsInfo.getPdtinfPdt()+"】"+"，开始执行时间【"+executionTime+"】"+"，执行价格【"+price+"】");
        tPriceRecordService.save(tPriceRecord);
    }
    }
